<template>
  <div :class="settings.ismobile ? 'mobile' : 'desktop'">
    <!-- 状态表单 -->
    <textarea
      class="textarea textarea-bordered"
      v-model="statusText"
      placeholder="状态"
    ></textarea>
    <!-- 快速提交 -->
    <div style="display: flex; flex-direction: column; margin-top: 1rem">
      <label for="data" class="block text-gray-700 font-semibold mb-2"
        >快速填入:</label
      >
      <input
        type="text"
        placeholder="快速填入表格"
        class="input input-bordered w-full max-w-xs"
        v-model="quickInput"
      />
      <button
        @click="handleQuickSubmit"
        class="btn w-full p-3 bg-green-500 text-white rounded hover:bg-green-600"
      >
        快速提交
      </button>
    </div>

    <!-- 注册表单 -->
    <form
      id="myForm"
      @submit.prevent="handleSubmit"
      class="flex w-full max-w-md bg-white p-6 rounded-lg shadow-md mt-4"
      style="display: flex; flex-direction: column; margin-top: 1rem"
    >
      <label
        class="input input-bordered"
        style="display: flex; align-items: center"
      >
        邮箱
        <input
          type="email"
          style="margin-left: 1rem"
          placeholder="请输入邮箱"
          v-model="formData.email"
          required
        />
      </label>

      <label
        class="input input-bordered"
        style="display: flex; align-items: center"
      >
        昵称
        <input
          type="text"
          style="margin-left: 1rem"
          placeholder="用户昵称"
          v-model="formData.nickname"
          required
        />
      </label>

      <label
        class="input input-bordered"
        style="display: flex; align-items: center"
      >
        密码
        <input
          type="password"
          style="margin-left: 1rem"
          placeholder="密码"
          v-model="formData.pass"
          required
        />
      </label>

      <label
        class="input input-bordered"
        style="display: flex; align-items: center"
      >
        API密钥
        <input
          type="password"
          style="margin-left: 1rem"
          placeholder="密钥"
          v-model="formData.token"
          required
        />
      </label>

      <button
        type="submit"
        class="btn w-full p-3 bg-green-500 text-white rounded hover:bg-green-600"
      >
        提交
      </button>
    </form>
  </div>
</template>

<script lang="ts">
import Method from '@/globalmethods'
import Cfg from '@/config/config'

export default {
  name: 'Zc',
  data() {
    return {
      settings: Cfg.set,
      statusText: '',
      quickInput: '',
      formData: {
        email: '',
        nickname: '',
        pass: '',
        token: '',
      },
    }
  },
  methods: {
    async handleQuickSubmit() {
      const data = this.quickInput.split(' ')
      this.statusText = ''

      for (const el of data) {
        const a = el.split('\t')
        await this.processData(a)
      }

      this.quickInput = ''
    },
    async processData(data: string[]) {
      if (!/\w+@qq.com/.test(data[2])) {
        this.statusText += `${data[0]} 邮箱不正确\n`
        return
      }

      this.formData.email = data[2]
      this.formData.nickname = data[0]
      this.formData.pass = data[3] === '随机' ? '123456789' : data[3]

      if (this.formData.pass.length < 6 || this.formData.pass.length > 16) {
        this.statusText += `${data[0]} 用户名或密码长度不能小于6位或超过16位\n`
        return
      }

      try {
        const res = await Method.axios.get(
          `https://suancaixianyu.cn/xyscapi/yh?uid=${encodeURIComponent(data[1])}`,
        )

        if (res.data.hmd) {
          this.statusText += `${data[0]} 黑名单用户\n`
          return
        } else {
          this.statusText += `${data[0]} 正常`

          const response = await Method.axios.post(
            'https://m.schub.top/user/adminRegister',
            this.formData,
          )

          this.statusText += ` ${response.data.msg}\n`
        }
      } catch (error) {
        alert('请求出错')
      }
    },
    async handleSubmit() {
      localStorage.setItem('myToken', this.formData.token)

      try {
        const response = await Method.axios.post(
          'https://m.schub.top/user/adminRegister',
          this.formData,
        )

        alert(response.data.msg)
      } catch (error) {
        console.log('请求失败:', error)
      }
    },
  },
  mounted() {
    const storedToken = localStorage.getItem('myToken')
    if (storedToken) {
      this.formData.token = storedToken
    }
  },
}
</script>

<style scoped>
.mobile {
  display: flex;
  flex-direction: column;
  padding: 1rem;
  margin-top: 1rem;
}
.desktop {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
  margin-top: 1rem;
}
.desktop textarea {
  width: 50%;
}
</style>
